package com.soft.mainsite.module.data.web;

import com.github.pagehelper.PageInfo;
import com.soft.mainsite.module.data.dto.DataQueryParams;
import com.soft.mainsite.module.data.entity.MainData;
import com.soft.mainsite.module.data.service.DataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: hjm
 * @Date: 2020/11/25 0:22
 * Stay hungry,stay foolish!
 */
@Api(tags = "后台管理系统资料相关接口", value = "dataController")
@Slf4j
@Controller
@RequestMapping(value = "/data")
public class DataController {

    @Resource
    private DataService dataService;

    @ApiOperation(value = "后台管理系统用，分页条件查询预约列表")
    @RequestMapping("/list")
    public String index(@RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageNumber,
                        @RequestParam(value = "pageSize", defaultValue = "30") Integer pageSize,
                        DataQueryParams queryParams, Model model) {

        PageInfo<MainData> pageInfo = dataService.pageDataByConditions(queryParams, pageNumber, pageSize);
        model.addAttribute("dataList", pageInfo);
        model.addAttribute("pojo", queryParams);

        return "mainData/data_list";
    }

    @ApiOperation(value = "后台管理系统用，上传文件保存资料接口")
    @PostMapping(value = "/save")
    @ResponseBody
    public String save(MainData mainData) {

        Integer dataId = mainData.getId();
        String restResponse;
        if (dataId == null) {
            restResponse = dataService.saveMainData(mainData);
            log.info("后台管理系统用，上传文件保存资料接口，资料dataId = 【{}】，新增资料 = 【{}】", null, restResponse);
        } else {
            restResponse = dataService.updateMainData(mainData);
            log.info("后台管理系统用，上传文件保存资料接口，资料dataId = 【{}】，更新资料 = 【{}】", dataId, restResponse);
        }

        return restResponse;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(Long[] ids) {
        return dataService.delete(ids);
    }
}
